package com.beiding.workordersystem.orders.generalorder.controller;

import com.beiding.workordersystem.common.standard.R;
import com.beiding.workordersystem.orders.generalorder.entity.OrderType;
import com.beiding.workordersystem.orders.generalorder.vo.OrderTypeVo;
import com.beiding.workordersystem.orders.generalorder.service.OrderTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/orderType")
@Api(tags = "工单分类管理")
public class OrderTypeController {

    @Autowired
    private OrderTypeService orderTypeService;

    @PostMapping("/save")
    @PreAuthorize("hasAnyAuthority('generalOrderType_save')")
    @ApiOperation("保存分类")
    R save(@RequestBody @Validated OrderTypeVo vo) {
        OrderType type = new OrderType();
        BeanUtils.copyProperties(vo, type);
        orderTypeService.save(type);
        return R.ok(type.getId());
    }

    @GetMapping("/delete")
    @ApiOperation("删除分类")
    @PreAuthorize("hasAnyAuthority('generalOrderType_delete')")
    R delete(@RequestParam String id) {
        orderTypeService.delete(id);
        return R.ok();
    }

    @GetMapping("/tree")
    @ApiOperation("获取树结构")
    @PreAuthorize("hasAnyAuthority('generalOrderType_get')")
    R tree(){
        return R.ok(orderTypeService.tree());
    }


}
